package com.faselbaum.stalkme.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.faselbaum.stalkme.models.BluetoothDeviceInfo;
import com.faselbaum.stalkme.models.ProfileInfo;
import com.faselbaum.stalkme.models.ProfileInfo.relationShip;

public class ProfileInfoStorage
{
	
	private static ProfileInfoStorage _instance = null;
	
	private ProfileInfoStorage(){};
	public static ProfileInfoStorage getInstance()
	{
		if(_instance==null) _instance = new ProfileInfoStorage();
		return _instance;
	}
	
	public boolean storeProfileInfo(ProfileInfo info)
	{
		try
		{
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost("http://teammuffinclan.dlinkddns.com/site/custom/stalkme.php");
			
			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair("action", "setProfile"));
			postParameters.add(new BasicNameValuePair("id", info.getBluetoothDeviceInfo().getMACAddress()));
			postParameters.add(new BasicNameValuePair("firstName", info.getFirstName()));
			postParameters.add(new BasicNameValuePair("lastName", info.getLastName()));
			postParameters.add(new BasicNameValuePair("shortDescription", info.getShortDescription()));
			postParameters.add(new BasicNameValuePair("interests", info.getInterests()));
			postParameters.add(new BasicNameValuePair("relationship", Integer.toString(info.getRelationShip().ordinal())));
			UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
			post.setEntity(formEntity);
			
			HttpResponse response = client.execute(post);
			// Get the response
			BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			String line = "";
			while ((line = rd.readLine()) != null) 
			{
				Log.d("HTTP Response", line);
			}
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean deleteProfileInfo(ProfileInfo info)
	{
		try
		{
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost("http://teammuffinclan.dlinkddns.com/site/custom/stalkme.php");
			
			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair("action", "removeProfile"));
			postParameters.add(new BasicNameValuePair("id", info.getBluetoothDeviceInfo().getMACAddress()));
			UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
			post.setEntity(formEntity);
			
			HttpResponse response = client.execute(post);
			// Get the response
			BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			String line = "";
			while ((line = rd.readLine()) != null) 
			{
				Log.d("HTTP Response", line);
			}
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public ProfileInfo getProfile(String macAddress)
	{
		ProfileInfo retVal = null;
		try
		{
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost("http://teammuffinclan.dlinkddns.com/site/custom/stalkme.php");
			
			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair("action", "getProfile"));
			postParameters.add(new BasicNameValuePair("id", macAddress));
			UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
			post.setEntity(formEntity);
			
			HttpResponse response = client.execute(post);
			// Get the response
			BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			String line = "";
			String responseValue = "";
			while ((line = rd.readLine()) != null) 
			{
				Log.d("HTTP Response", line);
				responseValue += line;
			}
			
			try
			{
				JSONObject jsonObject = new JSONObject(responseValue);
				if(jsonObject.length()>0)
				{
					jsonObject = jsonObject.getJSONObject("profile");
					if(jsonObject.length()>0)
					{
						retVal = new ProfileInfo();
						
						String firstName = jsonObject.getString("firstName");
						String lastName = jsonObject.getString("lastName");
						String shortDescription = jsonObject.getString("shortDescription");
						String interests = jsonObject.getString("interests");
						relationShip relationship = relationShip.values()[jsonObject.getInt("relationship")];
						
						retVal.setBluetoothDeviceInfo(new BluetoothDeviceInfo(macAddress,null,Short.MIN_VALUE,0));
						retVal.setFirstName(firstName);
						retVal.setLastName(lastName);
						retVal.setShortDescription(shortDescription);
						retVal.setInterests(interests);
						retVal.setRelationShip(relationship);
					}					
				}
				
			}
			catch (JSONException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return retVal;
	}
	
}
